#pragma once
#include <map>
#include "SettingsDef.h"
#include "BaseReader.h"

typedef std::map<int, CIniValue*> CIniValueMap;

class CIniHandler
{
public:
  struct SDataItem
  { const TCHAR* name; int id; EIniValueType type; const TCHAR* def; };

public:
  CIniHandler() : m_dataItems(NULL)
  {}
  ~CIniHandler()
  { Destroy(); }

  bool Create(const TCHAR* szFile, const SDataItem* dataItems);
  bool Create(CBaseReader& r, const SDataItem* dataItems);
  void Destroy();

  CIniValue* GetValue(int key);

public:
  const TCHAR* GetString(int key) const
  {
    const CIniValue* v = GetValue(key);
    return (v && v->m_type == ivString) ?
      ((CIniStringValue*)v)->m_val.GetString()
      : TEXT("");
  }
  int GetInt(int key) const
  {
    const CIniValue* v = GetValue(key);
    return (v && v->m_type == ivInt) ?
      ((CIniIntValue*)v)->m_val
      : -1;
  }
  bool GetBool(int key) const
  {
    const CIniValue* v = GetValue(key);
    return (v && v->m_type == ivBool) ?
      ((CIniBoolValue*)v)->m_val
      : false;
  }

  const CIniValue* GetValue(int key) const;

protected:
  bool Load(CBaseReader& r);

  void MapGenerate();
  void MapApply(const TCHAR* key, const TCHAR* val);

protected:
  CIniValueMap     m_settingsMap;
  const SDataItem* m_dataItems;
};


class CSettings
{
public:
  static bool Initialize(const TCHAR* szSettingsFile);
  static void Terminate();

  static CIniHandler& Settings()
  { return s_Settings; }

  // COMMON
  static CString GetAppFolder();
  static CString GetStoragePath(int cardIndex = 0);
  static bool    IsSmartphone()
  { return s_isSmartphone; }

protected:
  static CIniHandler s_Settings;
  static bool        s_isSmartphone;
};

#define SETTINGS CSettings::Settings()

